package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.Projecttype;
import com.itheima.mapper.ProjecttypeMapper;
import com.itheima.query.ProjectPageQuery;
import com.itheima.query.ProjectTypePageQuery;
import com.itheima.result.PageResult;
import com.itheima.service.ProjecttypeService;
import com.itheima.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guanhuaer
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/projectType")
public class ProjectTypeController {
    @Autowired
    private ProjecttypeMapper projecttypeMapper;
    @Autowired
    private ProjecttypeService projecttypeService;

    @GetMapping("/all")
    @ApiOperation("查询所有的项目分类")
    public Result projectTypeAll() {
        List<Projecttype> projecttypes = new ArrayList<>();
        projecttypes = projecttypeMapper.selectAll();
        return Result.success(projecttypes);
    }
    @GetMapping("/projectType")
    @ApiOperation("查询分类是否存在")
    public Result projectType(String projectType) {
        if (projectType==null || projectType==""){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        Projecttype projecttypes = projecttypeMapper.selectType(projectType);
        if (projecttypes!=null){
            return Result.error("分类名称已存在");
        }
        return Result.success();
    }

    @PostMapping("/projectTypeAdd")
    @ApiOperation("新增分类")
    public Result projectTypeAdd(@RequestBody Projecttype projectType) {
        if (projectType.getProjectType()==null || projectType.getProjectType()==""){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        projecttypeService.projectTypeAdd(projectType);
        return Result.success();
    }

    @PostMapping("/projectTypePage")
    @ApiOperation("分页查询")
    public Result projectTypePage(@RequestBody ProjectTypePageQuery pageQuery) {
        if (pageQuery.getPageSize()==null || pageQuery.getPageNum()==null){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        PageResult pageResult = projecttypeService.pageQuery(pageQuery);
        return Result.success(pageResult);
    }
    @PostMapping("/projectTypeDelects")
    @ApiOperation("分类删除")
    public Result projectTypeDelects(@RequestBody String Ids){
        String string = Ids.substring(8,Ids.length()-2);
        if (string.length()<1){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        projecttypeService.Delects(string);
        return Result.success();
    }

    @PostMapping("/projectTypeEdit")
    @ApiOperation("分类修改")
    public Result projectTypeEdit(@RequestBody Projecttype projecttype){
        if (projecttype.getProjectType()==null || projecttype.getId()==null){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        projecttype.setUpdateTime(LocalDateTime.now());
        projecttypeMapper.updateType(projecttype);
        return Result.success();
    }

    @PostMapping("/projectTypeAdds")
    @ApiOperation("分类批量增加")
    public Result projectTypeAdds(@RequestBody String fileName,String creator){
        String filename = fileName.split(":")[1].substring(1,fileName.split(":")[1].length()-2);
        if (filename==null || filename==""){
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        boolean isTrue = projecttypeService.projectTypeAdds(filename,creator);
        if (isTrue){
            return Result.success();
        }
        return Result.error("文件格式错误");
    }




}
